package com.plm.blogs.utils;

import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.Random;

/**
 * @author Limi
 * @create 2019-01-10 0:24
 */
public class Encrypt {

    /**
     * 明文密码进行加密
     * salt 盐值
     * @param password
     */
    public static String encrypt(String password, String salt) {
        String hashAlgorithmName = "MD5";//加密方式
        Object credentials = password;//密码
        int hashIterations = 10000;//加密的次数
        Object simpleHash = new SimpleHash(hashAlgorithmName, credentials,
                salt, hashIterations);
        return simpleHash.toString();
    }

    /**
     * 随机生成6位数字密码
     * @return String
     */
    public static String randomPass(){
        String password = "";
        Random random = new Random();
        for (int i=0;i<6;i++) {
            password += random.nextInt(10);
        }
        return password;
    }

    /**
     * 随机生成4位验证码
     * @return
     */
    public static String randomCode(){
        String code = "abcdefghijklmnopqrstuvwxyz";
        String activeCode = "";
        Random random = new Random();
        for (int i=0;i<4;i++) {
            activeCode += code.charAt(random.nextInt(26));
        }
        return activeCode;
    }

}
